<template>
    <div>
        <el-button @click="dialogTableVisible = true">添加</el-button>
        <el-dialog v-model="dialogTableVisible" title="选择体检项" :before-close="handleClose">
            <el-input v-model="searchByIdOrName" placeholder="输入搜索" :clearable="true">
            </el-input>
            <div class="table_box">
                <el-table :data="tableTestOption" :border="true" style="width: 100%" :highlight-current-row="false"
                    :cell-style="{ 'text-align': 'center', 'font-size': '12px' }" :header-cell-style="{
                        'text-align': 'center',
                        'background-color': '#f9fafc',
                        'color': '#666'
                    }" @selection-change="handleSelectionChange" ref="multipleTableRef">
                    <el-table-column type="selection" min-width="53" />
                    <el-table-column prop="projectid" label="项目ID" max-width="157" />
                    <el-table-column prop="projectname" label="项目名称" max-width="179" />
                    <el-table-column prop="testrange" label="检查范围" max-width="91" />
                    <el-table-column prop="checkupmeaning" label="检查意义" max-width="89" />
                    <el-table-column prop="suituse" label="适用标签" max-width="223" />
                    <el-table-column prop="originprice" label="原价" max-width="223" />
                    <el-table-column prop="saleprice" label="售价" max-width="223" />
                </el-table>
                <div class="bottom_box">
                    <div class="flagOption" v-for="(item, index) in checkedOption" :key="index">
                        {{ item.projectname }}
                        <span @click="cancelOption([item])">x</span>
                    </div>

                </div>
            </div>
            <template #footer>
                <span class="dialog-footer">
                    <el-button @click="cancelShow">取消</el-button>
                    <el-button @click="addOption">确认</el-button>
                </span>
            </template>
        </el-dialog>
    </div>
</template>

<script setup lang="ts">
import service from '@/utils/service';
import { reactive, ref, watch } from 'vue'
import { ElTable } from 'element-plus'
// 定义emits
const emits = defineEmits(["updateData", "hideClient"])
// 绑定搜索框数据
const searchByIdOrName = ref<string>()
// 对话框显示隐藏
const dialogTableVisible = ref(false)
// 获取表格
const multipleTableRef = ref<InstanceType<typeof ElTable>>()
// 表格数据
interface ITestOption {
    "projectid": string,
    "projectname": string,
    "testrange": string,
    "checkupmeaning": string,
    "suituse": string,
    "originprice": number,
    "saleprice": number,
    "id"?: number | string
}
const tableTestOption = reactive<ITestOption[]>([])
const checkedOption = reactive<ITestOption[]>([])
const getTableData = () => {
    service({
        url: `/textOption`,

    }).then(res => {
        if (res.data) {
            tableTestOption.length = 0
            tableTestOption.push(...res.data)
        }
    })
}
getTableData()
const handleSelectionChange = (val: ITestOption[]) => {
    checkedOption.length = 0
    checkedOption.push(...val)
    console.log(checkedOption);
}
// 点击标签关闭效果
const cancelOption = (rows: ITestOption[]) => {
    if (rows) {
        rows.forEach((row) => {
            // 切换每一行换中的状态
            multipleTableRef.value!.toggleRowSelection(row, false)
        })
    }
}
// 取消按钮
const cancelShow = () => {
    dialogTableVisible.value = false
    multipleTableRef.value!.clearSelection()
}
// 确认按钮
const addOption = () => {
    dialogTableVisible.value = false
    checkedOption.forEach(item => {
        delete item.id
        service({
            url: `/tableFormOption`,
            method: "post",
            data: {
                ...item
            }
        })
    })
    multipleTableRef.value!.clearSelection()
    emits("updateData")
}
// watch 监听实现模糊搜索

watch(searchByIdOrName, () => {
    console.log(1);

    service({
        url: `textOption/?q=${searchByIdOrName.value}`,
    }).then(res => {
        if (res.data) {
            tableTestOption.length = 0
            tableTestOption.push(...res.data)
        }
    })
})
const handleClose = () => {
    dialogTableVisible.value = false
    emits("hideClient")

}
</script>

<style scoped>
.el-button--text {
    margin-right: 15px;
}

:deep(.el-dialog) {
    width: 1003px;
    height: 590px;

}

:deep(.el-dialog .el-dialog__header) {
    background-color: #F2F2F2;
    margin-right: 0;
    border-bottom: 1px solid #e4e4e4;
}

:deep(.el-dialog .el-dialog__header .el-dialog__title) {
    color: #333;
    font-size: 16px;
    font-weight: bold;
}

:deep(.el-dialog__body) {
    border-bottom: 1px solid #e4e4e4;
}

.dialog-footer button:first-child {
    margin-right: 10px;
}

.el-button {

    box-sizing: border-box;
    width: 81px;
    height: 30px;
    border: 1px solid #cccccc;
    background-color: #fff;
    cursor: pointer;
    font-size: 12px;
    border-radius: 0;
    color: #000;
}

.el-button:hover {
    background-color: rgb(242, 242, 242);
}

.el-input {
    width: 180px;
    height: 35px;
    margin-bottom: 10px;
}

.table_box {
    height: 380px;
    overflow: auto;
    display: flex;
    flex-direction: column;
    justify-content: space-between;
}

.table_box .bottom_box {
    display: flex;
}

.table_box .bottom_box .flagOption {
    width: 76px;
    height: 32px;
    border: 1px solid #2188F9;
    color: #2188F9;
    background-color: #ecf5ff;
    font-size: 12px;
    border-radius: 5px;
    position: relative;
    margin-right: 8px;
    line-height: 32px;
    text-align: center;

}

.table_box .bottom_box .flagOption span {
    color: #000;
    font-size: 14px;
    position: absolute;
    border-radius: 50%;
    line-height: 10px;
    text-align: center;
    width: 12px;
    height: 12px;
    top: -6px;
    right: -6px;
    background-color: #e4e4e4;
    cursor: pointer;
}
</style>